import { defineStore } from 'pinia'

export const useCustomQueryStore = defineStore('customQuery', {
	state: () => ({
		formData: {
			id: '',
			dataConnectorId: '',
			datasetId: '',
			config: {
				sql: 'SELECT * FROM ',
				params: [],
				selectedTables: [],
				tableFields: {}
			}
		},
		queryResult: []
	}),
	actions: {
		setFormData(data) {
			if (data) {
				this.formData = {
					...this.formData,
					...data,
					config: {
						sql: data.config?.sql || 'SELECT * FROM ',
						params: data.config?.params || [],
						selectedTables: data.config?.selectedTables || [],
						tableFields: data.config?.tableFields || {}
					}
				}
			}
		},
		updateSql(sql) {
			this.formData.config.sql = sql
		},
		updateSelectedTables(tables) {
			this.formData.config.selectedTables = tables
		},
		updateTableFields(tableFields) {
			this.formData.config.tableFields = tableFields
		},
		updateParams(params) {
			this.formData.config.params = params
		},
		updateQueryResult(result) {
			this.queryResult = result
		}
	}
})
